package com.jingwei.activiti;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 经纬
 * @since 2025/1/2 13:11
 */
@SpringBootTest
public class testActiviti {

    @Autowired
    private TaskService taskService;

    @Autowired
    private RuntimeService runtimeService;


    /**
     * 部署流程
     */
    @Test
    public void a() {


        //1、创建（流程引擎）ProcessEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //2、获取RepositoryServcie(re表的服务层)\
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //3、使用service进行流程的部署，部署bpmn和png
        Deployment deploy = repositoryService.createDeployment()
                .name("工单提报")
                .addClasspathResource("bpmn/repairOrder.bpmn20.xml")
//                .addClasspathResource("bpmn/cc.bpmn20.png")
                .deploy();
    }


    /**
     * 查询流程列表
     */
    @Test
    public void ActivitiList() {
        //1、创建（流程引擎）ProcessEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //2、获取RepositoryServcie(re表的服务层)\
        RepositoryService repositoryService = processEngine.getRepositoryService();
        List<Deployment> list = repositoryService.createDeploymentQuery().list();
        list.forEach(deployment -> {
            System.out.println("流程部署id：" + deployment.getId());
            System.out.println("流程部署名称：" + deployment.getName());//bd3dbfee-c8d3-11ef-b5a6-fe854003188c
            System.out.println(deployment.getKey());
        });
    }


    ///***************************************
    //初始化流程实例
    @Test
    public void init() {
        //1、创建（流程引擎）ProcessEngine
        ProcessInstance test = runtimeService.startProcessInstanceByKey("repairOrder");
        String processDefinitionId = test.getProcessDefinitionId();
        System.out.println("流程定义id：" + processDefinitionId);//test:1:bd4fc150-c8d3-11ef-b5a6-fe854003188c

    }

    //初始化工单流程
    @Test
    public void initTask() {
        //1、创建（流程引擎）ProcessEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        Map<String, Object> repairOrder = new HashMap<String, Object>();
        repairOrder.put("repairOrderId", "123456");
        repairOrder.put("repairDevice", "空调");
        repairOrder.put("repairYy", "空调漏水了快修");
        repairOrder.put("repairType", "大修");
        repairOrder.put("fqwx", "小明");
        repairOrder.put("qyfzr", "区域负责人");

        //2、获取RuntimeService(ru表的服务层)\
        RuntimeService runtimeService = processEngine.getRuntimeService();
        runtimeService.startProcessInstanceByKey("repairOrder", "123456", repairOrder);
    }


    @Test
    public void taskList() {
        List<Task> list = taskService.createTaskQuery().list();
        list.forEach(a -> {
            System.out.println(a.toString());
        });
    }


    @Test
    public void myTaskList() {
        List<Task> list = taskService.createTaskQuery().taskName("发起维修工单").list();
        for (Task task : list) {
            System.out.println(task.toString());
        }
    }

    //查询工单内容
    @Test
    public void taskDetail() {
        List<Task> task = taskService.createTaskQuery().taskName("发起维修工单").list();
        System.out.println(task);
    }

    //查询名称为小明的任务
    @Test
    public void taskDetail2() {
        List<Task> task = taskService.createTaskQuery().taskAssignee("小明").list();
        System.out.println(task);
    }

}